﻿using MeterialManager.Aop.ExceptionProcess;
using MeterialManager.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace MeterialManager.Model
{
    public class WarehouseEntry
    {
        public int Id { get; set; }
        public string WarehouseCode { get; set; }
        public DateTime WarehouseDate { get; set; }
        public string Recipient { get; set; }
        public string MeterialCode { get; set; }
        public string MeterialName { get; set; }
        public decimal WarehouseNumber { get; set; }
        public string Unit { get; set; }
        public string Remark { get; set; }

        public static List<WarehouseEntry> ListAll()
        {
            List<WarehouseEntry> warehouseEntries = new List<WarehouseEntry>();
            DataTable dt = SqlHelper.ExecuteTable("SELECT we.Id,we.WarehouseCode,we.WarehouseDate,we.Recipient,wed.MeterialCode,wed.WarehouseNumber,wed.Remark,m.MeterialName,m.Unit from WarehouseEntrys we inner join WarehouseEntryDetails wed on we.WarehouseCode = wed.WarehouseCode left join Meterials m on wed.MeterialCode = m.MeterialCode");
            foreach (DataRow dr in dt.Rows)
            {
                warehouseEntries.Add(dr.DataRowToModel<WarehouseEntry>());
            }
            return warehouseEntries;
        }

        public static string WarehouseEntryInsert(params SqlParameter[] sqlParameters)
        {
            try
            {
                string rows = SqlHelper.ExcuteGetInsertDataQuery(
                    "Insert into WarehouseEntrys(WarehouseCode,WarehouseDate,Recipient) OUTPUT INSERTED.WarehouseCode Values(dbo.WarehouseCode_fun(Cast(@WarehouseDate As DATE)),Cast(@WarehouseDate As DATE),@Recipient)", sqlParameters);
                return rows;
            }
            catch
            {
                throw new SqlProcessException(301, "入库单插入失败1！");
            }
        }

        public static int WarehouseEntryDetailInsert(params SqlParameter[] sqlParameters)
        {
            try
            {
                int rows = SqlHelper.ExecuteNonQuery(
                    "Insert into WarehouseEntryDetails (WarehouseCode,MeterialCode,WarehouseNumber,Remark) Values(@WarehouseCode,@MeterialCode,@WarehouseNumber,@Remark)", sqlParameters);
                return rows;
            }
            catch
            {
                throw new SqlProcessException(302, "入库单插入失败2！");
                //throw new SqlProcessException(302, ex.Message);
            }
        }
        public static int MeterialUpdate(params SqlParameter[] sqlParameters)
        {
            try
            {
                int rows = SqlHelper.ExecuteNonQuery(
                    "Update Meterials Set Stocks = Stocks + @WNumber Where MeterialCode = @MCode", sqlParameters);
                return rows;
            }
            catch(Exception ex)
            {
                //throw new SqlProcessException(303, "物料数量更新失败！");
                throw new SqlProcessException(303, ex.Message);
            }
        }

        public static ResponseObject<int> Insert(dynamic warehouseEntry)
        {
            SqlParameter warehouseDate = new SqlParameter("@WarehouseDate",warehouseEntry.WarehouseDate.ToString("yyyy-MM-dd"));
            SqlParameter recipient = new SqlParameter("@Recipient",warehouseEntry.Recipient);
            SqlParameter meterialCode = new SqlParameter("@MeterialCode",warehouseEntry.MeterialCode);
            SqlParameter mCode = new SqlParameter("@MCode",warehouseEntry.MeterialCode);
            SqlParameter warehouseNumber = new SqlParameter("@WarehouseNumber",warehouseEntry.WarehouseNumber);
            SqlParameter wNumber = new SqlParameter("@WNumber",warehouseEntry.WarehouseNumber);
            SqlParameter remark = new SqlParameter("@Remark",warehouseEntry.Remark);
            try
            {
                string warehouseCode = WarehouseEntryInsert(warehouseDate, recipient);
                SqlParameter Code = new SqlParameter("@WarehouseCode", warehouseCode);
                int detailRows = WarehouseEntryDetailInsert(Code, meterialCode, warehouseNumber, remark);
                int updateRows = MeterialUpdate(wNumber,mCode);
            }
            catch(SqlProcessException ex)
            {
                return ResponseObject<int>.CreateFailureResponse(ex.ErrorMsg, ex.ErrorCode);
            }
            return ResponseObject<int>.CreateSuccessResponse(1);
        }

        public static ResponseObject<int> Update(dynamic warehouseEntry)
        {
            SqlParameter warehouseCode = new SqlParameter("@WarehouseCode", warehouseEntry.WarehouseCode);
            SqlParameter wCode = new SqlParameter("@WarehouseCode", warehouseEntry.WarehouseCode);
            SqlParameter recipient = new SqlParameter("@Recipient", warehouseEntry.Recipient);
            SqlParameter meterialCode = new SqlParameter("@MeterialCode", warehouseEntry.MeterialCode);
            SqlParameter mCode = new SqlParameter("@MCode", warehouseEntry.MeterialCode);
            SqlParameter warehouseNumber = new SqlParameter("@WarehouseNumber", warehouseEntry.WarehouseNumber);
            SqlParameter wNumber = new SqlParameter("@WNumber", warehouseEntry.WarehouseNumber);
            SqlParameter remark = new SqlParameter("@Remark", warehouseEntry.Remark);
            try
            {
                int entryRows = WarehouseEntryUpdate(recipient,warehouseCode);
                int detailRows = WarehouseEntryDetailUpdate(meterialCode, warehouseNumber, remark, wCode);
                int updateRows = MeterialUpdate(wNumber, mCode);
            }
            catch (SqlProcessException ex)
            {
                return ResponseObject<int>.CreateFailureResponse(ex.ErrorMsg, ex.ErrorCode);
            }
            return ResponseObject<int>.CreateSuccessResponse(1);
        }

        public static ResponseObject<int> Delete(dynamic warehouseEntry)
        {
            SqlParameter warehouseCode = new SqlParameter("@WarehouseCode", warehouseEntry.WarehouseCode);
            SqlParameter wCode = new SqlParameter("@wCode", warehouseEntry.WarehouseCode);
            SqlParameter meterialCode = new SqlParameter("@MCode", warehouseEntry.MeterialCode);
            SqlParameter warehouseNumber = new SqlParameter("@WNumber", warehouseEntry.WarehouseNumber);
            try
            {
                int detailRows = WarehouseEntryDetailDelete(wCode);
                int entryRows = WarehouseEntryDelete(warehouseCode);
                int updateRows = MeterialUpdate(warehouseNumber,meterialCode);
            }
            catch (SqlProcessException ex)
            {
                return ResponseObject<int>.CreateFailureResponse(ex.ErrorMsg, ex.ErrorCode);
            }
            return ResponseObject<int>.CreateSuccessResponse(1);
        }

        public static int WarehouseEntryUpdate(params SqlParameter[] sqlParameters)
        {
            try
            {
                int rows = SqlHelper.ExecuteNonQuery(
                    "Update WarehouseEntrys Set Recipient = @Recipient Where WarehouseCode = @WarehouseCode", sqlParameters);
                return rows;
            }
            catch(Exception ex)
            {
                throw new SqlProcessException(301, ex.Message);
                //throw new SqlProcessException(301, "入库单更新失败！");
            }
        }

        public static int WarehouseEntryDetailUpdate(params SqlParameter[] sqlParameters)
        {
            try
            {
                int rows = SqlHelper.ExecuteNonQuery(
                        "Update WarehouseEntryDetails Set MeterialCode=@MeterialCode,WarehouseNumber=WarehouseNumber+@WarehouseNumber,Remark=@Remark Where WarehouseCode=@WarehouseCode", sqlParameters);
                return rows;
            }
            catch(Exception ex)
            {
                throw new SqlProcessException(302, ex.Message);
                //throw new SqlProcessException(301, "入库单更新失败！可能是物料编号、入库数量或者备注输入有误！");
            }
        }

        public static int WarehouseEntryDelete(params SqlParameter[] sqlParameters)
        {
            try
            {
                int rows = SqlHelper.ExecuteNonQuery(
                    "Delete From WarehouseEntrys Where WarehouseCode = @WarehouseCode", sqlParameters);
                return rows;
            }
            catch
            {
                throw new SqlProcessException(301, "入库单删除失败！");
            }
        }

        public static int WarehouseEntryDetailDelete(params SqlParameter[] sqlParameters)
        {
            try
            {
                int rows = SqlHelper.ExecuteNonQuery(
                    "Delete From WarehouseEntryDetails Where WarehouseCode = @wCode", sqlParameters);
                return rows;
            }
            catch
            {
                throw new SqlProcessException(302, "入库单删除失败！可能是物料编号、入库数量或者备注输入有误！");
            }
        }

        public static List<string> GetDateListAll()
        {
            List<string> dateList = new List<string>();
            DataTable dt = SqlHelper.ExecuteTable("Select Distinct(WarehouseDate) from WarehouseEntrys");
            foreach (DataRow dr in dt.Rows)
            {
                dateList.Add(((DateTime)dr["WarehouseDate"]).ToString("yyyy-MM-dd"));
            }
            return dateList;
        }
    }
}
